package co.edu.distribuidos.advertisements;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;

import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Document;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredDocumentUtils;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.TextElement;

import net.jxta.document.Element;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author alvar-go@uniandes.edu.co
 */
public class MyAdvertisement extends AdvertisementTemplate{

    private final static String mimeType= "text/xml";
    
    public static final String nombreAdvertisementTag = "NombreRecurso";
    
    public static final String pipeAdvertisementTag="IdRecurso";
    
    public static final String extensionAdvertisementTag = "ExtensionRecurso";
    
    public static final String tamanioAdvertisementTag = "TamanioRecurso";
    
    public static final String fechaAdvertisementTag = "FechaRecurso";
    
    public static final String propietarioAdvertisementTag = "PropietarioRecurso";
    
    public static final String palabrasClavesTag = "PalabarasClaveRecurso";
    
    private final static String[] fields = {nombreAdvertisementTag, palabrasClavesTag};

    public MyAdvertisement(){
    	super();
    }
    
    public MyAdvertisement(InputStream  stream) throws IOException{
        super();
        StructuredTextDocument document= (StructuredTextDocument) StructuredDocumentFactory.newStructuredDocument( new MimeMediaType(mimeType), stream);
        readAdvertisement(document);
    }
    
    public MyAdvertisement(Element root){
		super();
		readAdvertisement((TextElement)root);
	}
    
    public Document getDocument(MimeMediaType asMimeType) throws IllegalArgumentException{
    	System.out.println("Datos Advertisement");
    	System.out.println(getAdvertisementType());
    	System.out.println(darPipeAdvertisement().toString());
        if((null!= darNombreAdvertisement()) && (null!= darPipeAdvertisement()) && (null!= darExtensionArchivo()) && (null!= darTamArchivo()) && (null!= darFechaArchivo()) && (null!= darPropietarioArchivo()) && (null!= darPalabrasClave()) ){
            StructuredDocument document= (StructuredDocument) StructuredDocumentFactory.newStructuredDocument(asMimeType, getAdvertisementType());
            Element element;

            element= document.createElement(nombreAdvertisementTag, darNombreAdvertisement());
            document.appendChild(element);
            
            element= document.createElement(extensionAdvertisementTag, darExtensionArchivo());
            document.appendChild(element);
            
            element= document.createElement(tamanioAdvertisementTag, darTamArchivo());
            document.appendChild(element);
            
            element= document.createElement(fechaAdvertisementTag, darFechaArchivo());
            document.appendChild(element);
            
            element= document.createElement(propietarioAdvertisementTag, darPropietarioArchivo());
            document.appendChild(element);
            
            element= document.createElement(palabrasClavesTag, darPalabrasClave());
            document.appendChild(element);

            PipeAdvertisement pipe = darPipeAdvertisement();
            if(pipe!=null){
                StructuredTextDocument advDoc = (StructuredTextDocument) pipe.getDocument(asMimeType);
                StructuredDocumentUtils.copyElements(document, document, advDoc);
            }
            return document;
        }
        else{
            throw new IllegalArgumentException("faltan datos para hacer advert");
        }
    }

	public void readAdvertisement(TextElement document)throws IllegalArgumentException{
        if(document.getName().equals(getAdvertisementType())){
            Enumeration elements= document.getChildren();

            while(elements.hasMoreElements()){
                TextElement element= (TextElement) elements.nextElement();

                if(element.getName().equals(PipeAdvertisement.getAdvertisementType())){
                    PipeAdvertisement pipe = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(element);
                    cambiarPipeAdvertisement(pipe);
                    continue;
                }
                if(element.getName().equals(nombreAdvertisementTag)){
                    cambiarNombreAdvertisement(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(extensionAdvertisementTag)){
                    cambiarExtensionArchivo(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(tamanioAdvertisementTag)){
                    cambiarTamArchivo(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(fechaAdvertisementTag)){
                    cambiarFechaArchivo(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(propietarioAdvertisementTag)){
                    cambiarPropietarioArchivo(element.getTextValue());
                    continue;
                }
                if(element.getName().equals(palabrasClavesTag)){
                    cambiarPalabrasClave(element.getTextValue());
                    continue;
                }
            }
        }
        else{
            throw new IllegalArgumentException("no es el advertisement de recurso");
        }
    }
    
    @Override
    public String[] getIndexFields() {
        return fields;
    }
   

     public static class Instantiator implements AdvertisementFactory.Instantiator{

        public String getAdvertisementType() {
           return AdvertisementTemplate.getAdvertisementType();
        }

        public Advertisement newInstance() {
            return new MyAdvertisement();
        }

        public Advertisement newInstance(Element root) {
            return new MyAdvertisement(root);
        }

    };

}
